package gui;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.JScrollPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.border.TitledBorder;
import javax.swing.JComboBox;

import negocio.Cursada;
import negocio.CursadaControlador;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Cursadas extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTable table;
	private Cursadas padreCursadas;
	private CursadasCrear cursadasCrear= null;
	private CursadasExaminar cursadasExaminar=null;
	private NotasYExamenes notasYExamenes=null;
	private ClasesCrear clasesCrear = null;
	private Clases clases = null;
	
	private ArrayList<Cursada> cursadas;

	
	public void nullCursadasCrear()
	{
		cursadasCrear=null;
	}
	
	public void nullCursadasExaminar()
	{
		cursadasExaminar=null;
		
	}
	
	public void nullClases()
	{
		clases = null;
	}
	
	public void nullNotasYExamenes()
	{
		notasYExamenes=null;
	}
	
	public void nullClasesCrear()
	{
		this.clasesCrear = null;
	}
	
	

	/**
	 * Launch the application.
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Cursadas frame = new Cursadas();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	 */
	
	/**
	 * Create the frame.
	 */
	public Cursadas(Principal padre) {
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				padre.nullCursadas();
			}
		});
		padreCursadas=this;
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(270, 220, 540, 505);
		this.setTitle("Cursadas");
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Busqueda", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(14, 17, 500, 105);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel lblCodigoCursada = new JLabel("Codigo Cursada");
		lblCodigoCursada.setBounds(6, 19, 83, 14);
		panel.add(lblCodigoCursada);
		
		JLabel lblCurso = new JLabel("Curso");
		lblCurso.setBounds(286, 19, 46, 14);
		panel.add(lblCurso);
		
		JLabel lblArea = new JLabel("Area");
		lblArea.setBounds(286, 44, 46, 14);
		panel.add(lblArea);
		
		JLabel lblInstructor = new JLabel("Instructor");
		lblInstructor.setBounds(6, 44, 83, 14);
		panel.add(lblInstructor);
		
		JLabel lblAlumno = new JLabel("Alumno");
		lblAlumno.setBounds(6, 72, 46, 14);
		panel.add(lblAlumno);
		
		textField = new JTextField();
		textField.setBounds(99, 16, 86, 20);
		panel.add(textField);
		textField.setColumns(10);
		
		textField_3 = new JTextField();
		textField_3.setBounds(99, 41, 86, 20);
		panel.add(textField_3);
		textField_3.setColumns(10);
		
		textField_4 = new JTextField();
		textField_4.setBounds(99, 69, 86, 20);
		panel.add(textField_4);
		textField_4.setColumns(10);
		
		JButton btnBuscar = new JButton("Buscar");
		btnBuscar.setBounds(401, 71, 89, 23);
		panel.add(btnBuscar);
		
		JComboBox comboBox = new JComboBox();
		comboBox.setBounds(342, 19, 148, 20);
		panel.add(comboBox);
		
		JComboBox comboBox_1 = new JComboBox();
		comboBox_1.setBounds(342, 44, 148, 20);
		panel.add(comboBox_1);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "Resultados de busqueda", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_1.setBounds(10, 133, 504, 225);
		contentPane.add(panel_1);
		panel_1.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
			}
		});
		scrollPane.setBounds(10, 16, 484, 198);
		panel_1.add(scrollPane);
		
		table = new JTable();
		llenarTabla();
		scrollPane.setViewportView(table);
		
		
		
		JButton btnCrearNueva = new JButton("Crear...");
		btnCrearNueva.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(cursadasCrear==null)
					cursadasCrear= new CursadasCrear(padreCursadas);
				cursadasCrear.setVisible(true);
			}
		});
		btnCrearNueva.setBounds(7, 369, 122, 23);
		contentPane.add(btnCrearNueva);
		
		JButton button = new JButton("Editar...");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		button.setBounds(135, 369, 122, 23);
		contentPane.add(button);
		
		JButton btnExaminar = new JButton("Examinar");
		btnExaminar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(cursadasExaminar==null)
					cursadasExaminar = new CursadasExaminar(padreCursadas);
				cursadasExaminar.setVisible(true);
			}
		});
		btnExaminar.setBounds(264, 369, 122, 23);
		contentPane.add(btnExaminar);
		
		JButton btnReportarNotas = new JButton("Notas y examenes");
		btnReportarNotas.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(notasYExamenes==null)
					notasYExamenes=new NotasYExamenes(padreCursadas, cursadas.get(table.getSelectedRow()));
				notasYExamenes.setVisible(true);	
			}
		});
		btnReportarNotas.setBounds(393, 369, 122, 23);
		contentPane.add(btnReportarNotas);
		
		JButton btnSalir = new JButton("Salir");
		btnSalir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnSalir.setBounds(425, 433, 89, 23);
		contentPane.add(btnSalir);
		
		JButton btnNewButton = new JButton("Asistencias");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(clases == null)
				{
					clases = new Clases(padreCursadas,cursadas.get(table.getSelectedRow()));
				}
				clases.setVisible(true);
			}
		});
		btnNewButton.setBounds(135, 403, 122, 23);
		contentPane.add(btnNewButton);
		
		JButton btnCrearClases = new JButton("Crear clases");
		btnCrearClases.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0)
			{
				if(clasesCrear == null)
				{
					clasesCrear = new ClasesCrear(padreCursadas, cursadas.get(table.getSelectedRow()));
				}
				clasesCrear.setVisible(true);
			}
		});
		btnCrearClases.setBounds(7, 403, 122, 23);
		contentPane.add(btnCrearClases);
		
		if (cursadas.size()>0)
		{
			table.changeSelection(0, 0, false, false);
		}
		else
		{
			button.setVisible(false);
			btnNewButton.setVisible(false);
			btnCrearClases.setVisible(false);
			btnReportarNotas.setVisible(false);
			btnReportarNotas.setVisible(false);			
		}
	}
	
	public void llenarTabla()
	{
		cursadas = new ArrayList<Cursada>();
		DefaultTableModel tm = new DefaultTableModel();
		Object[] encabezado = {"Codigo cursada","Curso","Fecha de Inicio", "Horas","Instructor"};
		tm.setColumnIdentifiers(encabezado);
		cursadas = CursadaControlador.getAll();
		for(Cursada c : cursadas)
		{
			Object[] fila = {c.getCodigo(),c.getCurso().getNombre(),c.getFechaInicio().toString(),c.getHoras(),c.getInstructor().toString()};
			tm.addRow(fila);
		}
		table.setModel(tm);
	}
}
